/**
 * Created by y on 2016/12/29.
 */
$(function () {
    InitLeftMenu();
    tabClose();
    tabCloseEven();
})
//初始化左侧
function InitLeftMenu() {
    var _menus = {
        "menus": [
            {
                "menuid": "1", "icon": "icon-sys", "menuname": "系统管理",
                "menus": [{"menuname": "菜单管理", "icon": "icon-nav", "url": "demo.html"},
                    {"menuname": "用户管理", "icon": "icon-users", "url": "http://localhost:8090/gbzdyf-web/user/ls"},
                    {"menuname": "角色管理", "icon": "icon-role", "url": "demo2.html"},
                    {"menuname": "权限设置", "icon": "icon-set", "url": "demo.html"},
                    {"menuname": "系统日志", "icon": "icon-log", "url": "demo.html"}
                ]
            }, {
                "menuid": "8", "icon": "icon-sys", "menuname": "员工管理",
                "menus": [{"menuname": "员工列表", "icon": "icon-nav", "url": "demo.html"},
                    {"menuname": "视频监控", "icon": "icon-nav", "url": "demo1.html"}
                ]
            }, {
                "menuid": "56", "icon": "icon-sys", "menuname": "部门管理",
                "menus": [{"menuname": "添加部门", "icon": "icon-nav", "url": "demo1.html"},
                    {"menuname": "部门列表", "icon": "icon-nav", "url": "demo2.html"}
                ]
            }, {
                "menuid": "28", "icon": "icon-sys", "menuname": "财务管理",
                "menus": [{"menuname": "收支分类", "icon": "icon-nav", "url": "demo.html"},
                    {"menuname": "报表统计", "icon": "icon-nav", "url": "demo1.html"},
                    {"menuname": "添加支出", "icon": "icon-nav", "url": "demo.html"}
                ]
            }, {
                "menuid": "39", "icon": "icon-sys", "menuname": "商城管理",
                "menus": [{"menuname": "商品分", "icon": "icon-nav", "url": "demo.html"},
                    {"menuname": "商品列表", "icon": "icon-nav", "url": "demo.html"},
                    {"menuname": "商品订单", "icon": "icon-nav", "url": "demo.html"}
                ]
            }
        ]
    };

    $(".easy-ui_menu").empty();
    var menuList = "";
    $.each(_menus.menus, function (i, n) {
        menuList += '<div title="' + n.menuname + '"  icon="' + n.icon + '" style="overflow:auto;">';
        menuList += '<ul>';
        $.each(n.menus, function (j, o) {
            menuList += '<li><div><a target="mainFrame" href="' + o.url + '" ><span class="icon ' + o.icon + '" ></span>' + o.menuname + '</a></div></li> ';
        })
        menuList += '</ul></div>';
    })

    $(".easy-ui_menu").append(menuList);

    $('.easy-ui_menu li a').click(function () {
        var tabTitle = $(this).text();
        var url = $(this).attr("href");
        addTab(tabTitle, url);
        $('.easy-ui_menu li div').removeClass("selected");
        $(this).parent().addClass("selected");
    }).hover(function () {
        $(this).parent().addClass("hover");
    }, function () {
        $(this).parent().removeClass("hover");
    });

    $(".easy-ui_menu").accordion();
};

function addTab(subtitle, url) {
    if (!$('#tabs').tabs('exists', subtitle)) {
        $('#tabs').tabs('add', {
            title: subtitle,
            content: createFrame(url),
            closable: true,
            width: $('#mainPanle').width() - 10,
            height: $('#mainPanle').height() - 100
        });
    } else {
        $('#tabs').tabs('select', subtitle);
    }
    tabClose();
}


function createFrame(url) {
    var s = '<iframe name="mainFrame" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
    return s;
}

//双击关闭TAB选项卡
function tabClose() {
    $(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children("span").text();
        $('#tabs').tabs('close', subtitle);
    })

    $(".tabs-inner").bind('contextmenu', function (e) {
        $('#mm').menu('show', {
            left: e.pageX,
            top: e.pageY
        });

        var subtitle = $(this).children("span").text();
        $('#mm').data("currtab", subtitle);

        return false;
    });
};

//绑定右键菜单事件
function tabCloseEven() {
    //关闭当前
    $('#mm-tabclose').click(function () {
        var currtab_title = $('#mm').data("currtab");
        $('#tabs').tabs('close', currtab_title);
    })
    //全部关闭
    $('#mm-tabcloseall').click(function () {
        $('.tabs-inner span').each(function (i, n) {
            var t = $(n).text();
            $('#tabs').tabs('close', t);
        });
    });
    //关闭除当前之外的TAB
    $('#mm-tabcloseother').click(function () {
        var currtab_title = $('#mm').data("currtab");
        $('.tabs-inner span').each(function (i, n) {
            var t = $(n).text();
            if (t != currtab_title)
                $('#tabs').tabs('close', t);
        });
    });
    //关闭当前右侧的TAB
    $('#mm-tabcloseright').click(function () {
        var nextall = $('.tabs-selected').nextAll();
        if (nextall.length == 0) {
            //msgShow('系统提示','后边没有啦~~','error');
            alert('后边没有啦~~');
            return false;
        }
        nextall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            $('#tabs').tabs('close', t);
        });
        return false;
    });
    //关闭当前左侧的TAB
    $('#mm-tabcloseleft').click(function () {
        var prevall = $('.tabs-selected').prevAll();
        if (prevall.length == 0) {
            alert('到头了，前边没有啦~~');
            return false;
        }
        prevall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            $('#tabs').tabs('close', t);
        });
        return false;
    });
    //退出
    $("#mm-exit").click(function () {
        $('#mm').menu('hide');
    })
}



